#!/usr/bin/python2.6
# -*- coding: UTF-8 -*-

import os
import time
import argparse


# *.py --src_list upload.log --tar_list cdn_refresh.log --http_url http://manage-test.linkfun.top
# ----------------------------------------------------main------------------------------------------------
def parse_args():
    parser = argparse.ArgumentParser(description='修改上传日志工具.\n')
    parser.add_argument('--http_url', dest='http_url', type=str, required=True, default="", help='网站地址')
    parser.add_argument('--src_list', dest='src_list', type=str, required=True, default="upload.log", help='需要转换的七牛上传日志')
    parser.add_argument('--tar_list', dest='tar_list', type=str, required=True, default="cdn_refresh.log",help='导出转换后的路径')
    args = parser.parse_args()
    return args


def main():
    startTime = time.time()
    app_args = parse_args()

    src_list = app_args.src_list
    http_url  = app_args.http_url
    tar_list = app_args.tar_list
    extList = ['.js','.ttf','.html','.css']
    lineList = []
    fileObj = open(src_list)
    while 1:
        line = fileObj.readline()
        num = line.rfind(": ")
        if num != -1 and  line.rfind("success") != -1:
            newLine = line[ num + 2:]
            for extName in extList:
                if newLine.find(extName) != -1:
                    newLine = newLine[:newLine.find(extName) + len(extName)]
                    lineList.append('/' + newLine)

        if not line:
            break
    fileObj.close()


    fixList = []
    fixList.append(http_url + '\n')
    for line in lineList:
        newLine = http_url + line + '\n'
        fixList.append(newLine)
        print(newLine)

    if os.path.exists(tar_list):
        os.remove(tar_list)

    fileObj = open(tar_list,'w')
    fileObj.writelines(fixList)
    fileObj.close()


    endTime = time.time()
    print ("总耗时: %0.2f" % (endTime - startTime))


if __name__ == "__main__":
    main()